//
//  FeedDetailController.m
//  collection
//
//  Created by 靖 张 on 11/26/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "FeedDetailController.h"
#import "PublishCommentController.h"
#import "ViewPictureController.h"
#import "CollectionDataManager.h"
#import "LoginController.h"
#import "RegisterController.h"
#import "UserProfileController.h"
#import "Utility.h"
#import "AppDelegate.h"
#import "UserInfo.h"
#import "MBProgressHUDHelper.h"
#import "FeedListViewController.h"
#import "UserProfileController.h"
#import <QuartzCore/QuartzCore.h>

@interface FeedDetailController()

@property (nonatomic, strong)   NSArray*    commentMsgList;

- (IBAction)onClickBackBarItem:(id)sender;
- (IBAction)onClickComment:(id)sender;
- (IBAction)onClickForward:(id)sender;
- (IBAction)onClickLike:(id)sender;
- (void)onClickImage:(UIGestureRecognizer*)gestureRecognizer;
- (void)onClickBody:(UITapGestureRecognizer*)recognizer;

- (void)onClickTagLabel:(UITapGestureRecognizer*)gestureRecognizer;
- (void)onClickCommentLabel:(UITapGestureRecognizer*)gestureRecognizer;
- (void)onClickForwardLabel:(UITapGestureRecognizer*)gestureRecognizer;
- (void)onClickLikeLabel:(UITapGestureRecognizer*)gestureRecognizer;
- (void)onClickCellItem:(UITapGestureRecognizer*)gestureRecognizer;

- (IBAction)onClickPrevious:(id)sender;
- (IBAction)onCLickNext:(id)sender;

- (void)buildBodyView;

- (void)loadCommentMsg;

@end

@implementation FeedDetailController

@synthesize feed = _feed;
@synthesize commentMsgList  = _commentMsgList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.hidesBottomBarWhenPushed = YES;
        self.title  = @"详情";
        
        //    self.navigationController.navigationBarHidden = NO;
       
        UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_detail_on"]];
        self.navigationItem.titleView = imgView;
        [imgView release];
    }
    
    return self;
}

- (void)dealloc {
    
    self.feed = nil;
    self.focusPicName = nil;
    self.commentMsgList = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView {
    
    [super loadView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // Load body view
    [self buildBodyView];
    [self loadCommentMsg];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.commentMsgList count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* cellID = @"FeedDetailCommentCell";
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"FeedDetailCommentCell" owner:self options:nil];
        cell = [views objectAtIndex:0];
        UIView* bgView = [[UIView alloc] initWithFrame:cell.frame];
        bgView.backgroundColor = [UIColor colorWithRed:229.0 / 255.0 green:229.0 / 255.0 blue:229.0 / 255.0 alpha:1];
        cell.selectedBackgroundView = bgView;
        [bgView release];
    }
    
    // Customize the cell
    Feed* comment = [self.commentMsgList objectAtIndex:indexPath.row];
    
    UIImageView* avatarView = (UIImageView*)[cell viewWithTag:1];
    avatarView.image = comment.avatar;
    avatarView.userInteractionEnabled = YES;
    if (avatarView.gestureRecognizers.count == 0) {
        UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCellItem:)];
        [avatarView addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];
    }
    
    UILabel* nicknameLabel = (UILabel*)[cell viewWithTag:2];
    nicknameLabel.text = comment.nickname;
    nicknameLabel.userInteractionEnabled = YES;
    if (nicknameLabel.gestureRecognizers.count == 0) {
        UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCellItem:)];
        [nicknameLabel addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];
    }
    
    UILabel* timeLabel = (UILabel*)[cell viewWithTag:3];
    timeLabel.text = [Utility transTimeToCurrentFormat:comment.publishTime];
    
    UILabel* commentLabel = (UILabel*)[cell viewWithTag:4];
    commentLabel.text = comment.text;
    [commentLabel sizeToFit];
    
    CGRect frame = cell.frame;
    frame.size.height = 10 + nicknameLabel.frame.size.height + commentLabel.frame.size.height + 8;
    cell.frame = frame;
    
    return cell;
}

#pragma mark - Implementation of UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}
/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Feed* feed = [self.commentMsgList objectAtIndex:indexPath.row];
    FeedDetailController* feedDetailController = [[FeedDetailController alloc] initWithNibName:@"FeedDetailController" bundle:nil];
    feedDetailController.feed = feed;
    [self.navigationController pushViewController:feedDetailController animated:YES];
    [feedDetailController release];
}
*/
#pragma mark - Private methods

- (IBAction)onClickBackBarItem:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    //    self.navigationController.navigationBarHidden = YES;
}

- (void)onClickCellItem:(UITapGestureRecognizer *)gestureRecognizer {
    
    UIView* subView = gestureRecognizer.view;
    UITableViewCell* cell = (UITableViewCell*)subView.superview.superview;
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:10];
    NSIndexPath* indexPath = [tableView indexPathForCell:cell];
    assert(indexPath.section >= 0);
    
    Feed* comment = [self.commentMsgList objectAtIndex:indexPath.row];
    UserProfileController* userProfileController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
    userProfileController.userID = comment.userID;
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:userProfileController animated:YES];
    [userProfileController release];
}

- (IBAction)onClickComment:(id)sender {
    
    PublishCommentController* publishCommentController = [[PublishCommentController alloc] initWithNibName:@"PublishCommentController" bundle:nil];
    publishCommentController.sourceFeed = self.feed;
    publishCommentController.isCommentOrForward = YES;
    [self presentModalViewController:publishCommentController animated:YES];
    [publishCommentController release];
}

- (IBAction)onClickForward:(id)sender {
    
    PublishCommentController* publishCommentController = [[PublishCommentController alloc] initWithNibName:@"PublishCommentController" bundle:nil];
    publishCommentController.sourceFeed = self.feed;
    publishCommentController.isCommentOrForward = NO;
    [self presentModalViewController:publishCommentController animated:YES];
    [publishCommentController release];
}

- (IBAction)onClickLike:(id)sender {
    
    MBProgressHUD* likeTip = [MBProgressHUDHelper popUpMBProcessHUD:self.view delegate:nil];
    [MBProgressHUDHelper changeToMBSuccessMode:likeTip customMsg:@"已标记为喜欢"];
    
    self.feed.favorNum++;
    UIView* bodyView = [self.view viewWithTag:10];
    UIView* timeAreaView = (UIView*)[bodyView viewWithTag:61];
    UILabel* favLabel = (UILabel*)[timeAreaView viewWithTag:7];
    favLabel.text = [NSString stringWithFormat:@"%lld", self.feed.favorNum];
}

- (void)onClickTagLabel:(UITapGestureRecognizer *)gestureRecognizer {
    
    UILabel* tagLabel = (UILabel*)gestureRecognizer.view;
    NSString* labelText = tagLabel.text;
    
    FeedListViewController* feedListController = [[FeedListViewController alloc] initWithNibName:@"FeedListViewController" bundle:nil];
    feedListController.tag = labelText;
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:feedListController animated:YES];
    [feedListController release];
}

- (void)onClickCommentLabel:(UITapGestureRecognizer *)gestureRecognizer {
    
    [self onClickComment:nil];
}

- (void)onClickForwardLabel:(UITapGestureRecognizer *)gestureRecognizer {
    
    [self onClickForward:nil];
}

- (void)onClickLikeLabel:(UITapGestureRecognizer *)gestureRecognizer {
    
    [self onClickLike:nil];
}

- (void)onClickImage:(UIGestureRecognizer *)gestureRecognizer {
    
    ViewPictureController* viewPictureController = [[ViewPictureController alloc] initWithNibName:@"ViewPictureController" bundle:nil];
    if (self.feed.originalFeed.pictures != nil && self.feed.originalFeed.pictures.count > 0) {
        viewPictureController.picArray = self.feed.originalFeed.pictures;
    }
    else {
        viewPictureController.picArray = self.feed.pictures;
    }
    
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController presentModalViewController:viewPictureController animated:YES];
    [viewPictureController release];
}

- (IBAction)onClickPrevious:(id)sender {
    
}

- (IBAction)onCLickNext:(id)sender {
    
}

- (void)onClickBody:(UITapGestureRecognizer *)recognizer {
    
    UIView* bodyView = [self.view viewWithTag:10];
    CGPoint point = [recognizer locationInView:bodyView];
    NSLog(@"Location : %f, %f", point.x, point.y);
    UIImageView* separator = (UIImageView*)[bodyView viewWithTag:13];
    CGRect separatorFrame = separator.frame;
    if (separatorFrame.origin.y + separatorFrame.size.height <= point.y) {
        return;
    }
    
    UserProfileController* userProfileController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
    userProfileController.userID = self.feed.userID;
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:userProfileController animated:YES];
    [userProfileController release];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSLog(@"scrollview.tag : %d", scrollView.tag);
    if (scrollView.tag == 10) {
        UIView* parentView = scrollView.superview;
        UIPageControl* pageIndicator = (UIPageControl*)[parentView viewWithTag:20];
        NSLog(@"contentOffset, x = %f, y = %f", scrollView.contentOffset.x, scrollView.contentOffset.y);
        int index = scrollView.contentOffset.x / scrollView.frame.size.width;
        pageIndicator.currentPage = index;
    }
}

#pragma mark [ClickCellDelegate]

- (void)onClickCell:(UITableViewCell *)cell ctrl:(id)subView {
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:10];
    NSIndexPath* indexPath = [tableView indexPathForCell:cell];
    assert(indexPath.section >= 0);
    
    Feed* comment = [self.commentMsgList objectAtIndex:indexPath.row];
    UserProfileController* userProfileController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
    userProfileController.userID = comment.userID;
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:userProfileController animated:YES];
    [userProfileController release];
}

- (void)buildBodyView {
    
    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"FeedDetailBodyView" owner:self options:nil];
    UIView* bodyView = (UIView*)[views objectAtIndex:0];
    bodyView.backgroundColor = [UIColor colorWithRed:249.0 / 255.0 green:249.0 / 255.0 blue:249.0 / 255.0 alpha:1];
    bodyView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    if (self.feed == nil) {
        NSLog(@"No feed found!");
        return;
    }
    
    CGRect frame;
    
    // Avatar
    UIImageView* avatarView = (UIImageView*)[bodyView viewWithTag:11];
    avatarView.image = self.feed.avatar;
    
    // Nickname
    UILabel* nicknameLabel = (UILabel*)[bodyView viewWithTag:12];
    nicknameLabel.text  = self.feed.nickname;
    
    // Separator
    UIImageView* separator = (UIImageView*)[bodyView viewWithTag:13];
    
    // Note
    UILabel* noteLabel = (UILabel*)[bodyView viewWithTag:14];
    frame = noteLabel.frame;
    if (self.feed.text != nil && ![self.feed.text isEqualToString:@""]) {
        noteLabel.text = self.feed.text;
        [noteLabel sizeToFit];
        frame = noteLabel.frame;
        frame.origin.y = separator.frame.origin.y + separator.frame.size.height;
        noteLabel.frame = frame;
    }
    else {
        frame = noteLabel.frame;
        frame.size = CGSizeZero;
        noteLabel.frame = frame;
    }
    
    // Image area
    UIView* imageAreaView = (UIView*)[bodyView viewWithTag:31];
    frame = imageAreaView.frame;
    frame.origin.y = noteLabel.frame.origin.y + noteLabel.frame.size.height + 8;
    imageAreaView.frame = frame;
    if (self.feed.pictures != nil && [self.feed.pictures count] > 0) {
        UIImage* img = [UIImage imageNamed:[self.feed.pictures objectAtIndex:0]];
        CGSize picSize = img.size;
        UIScrollView* imageContainerView = (UIScrollView*)[imageAreaView viewWithTag:10];
        CGSize picAreaSize = CGSizeMake(imageAreaView.frame.size.width, picSize.height * imageAreaView.frame.size.width / picSize.width);
        frame.size = picAreaSize;
        imageAreaView.frame = frame;

        imageContainerView.contentSize = CGSizeMake(self.feed.pictures.count * picAreaSize.width, picAreaSize.height);
        CGRect imageAreaFrame = CGRectMake(0, 0, picAreaSize.width, picAreaSize.height);
        imageContainerView.frame = imageAreaFrame;
        
        imageContainerView.delegate = self;
        // Add pictures
        for (int i = 0; i < self.feed.pictures.count; i++) {
            NSString* picName = [self.feed.pictures objectAtIndex:i];
            UIImage* img = [UIImage imageNamed:picName];
            CGRect imgRect = CGRectMake(i * picAreaSize.width, 0, picAreaSize.width, picAreaSize.height);
            UIImageView* imgView = [[UIImageView alloc] initWithFrame:imgRect];
            imgView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)];
            [imgView addGestureRecognizer:tapGestureRecognizer];
            [tapGestureRecognizer release];
            
            imgView.image = img;
            [imageContainerView addSubview:imgView];
            [imgView release];
        }
        
        UIPageControl* imageIndexIndicator = (UIPageControl*)[imageAreaView viewWithTag:20];
        imageIndexIndicator.numberOfPages = [self.feed.pictures count];
        CGSize size = CGSizeMake(imageIndexIndicator.numberOfPages * 15, 10);
        imageIndexIndicator.currentPage = 0;
        CGRect imgIndicatorFrame = CGRectMake(imageAreaFrame.size.width - 10 - size.width, imageAreaFrame.size.height - imageIndexIndicator.frame.size.height, size.width, imageIndexIndicator.frame.size.height);
        imageIndexIndicator.frame = imgIndicatorFrame;
        
        //
        if (self.focusPicName != nil) {
            int index = -1;
            for (int i = 0; i < self.feed.pictures.count; i++) {
                NSString* picName = [self.feed.pictures objectAtIndex:i];
                if ([picName isEqualToString:self.focusPicName]) {
                    index = i;
                    break;
                }
            }
            
            if (index != -1) {
                [imageContainerView setContentOffset:CGPointMake(picAreaSize.width * index, 0) animated:NO];
                [imageIndexIndicator setCurrentPage:index];
            }
        }
    }
    else {
        for (UIView* subView in imageAreaView.subviews) {
            [subView removeFromSuperview];
        }
        imageAreaView.frame = CGRectMake(10, noteLabel.frame.origin.y + noteLabel.frame.size.height, 320 - 10 - 10, 0);
    }
    
    // Source area
    UIView* sourceAreaView = (UIView*)[bodyView viewWithTag:41];
    CGRect sourceAreaFrame = sourceAreaView.frame;
    sourceAreaFrame.origin.y = imageAreaView.frame.origin.y + imageAreaView.frame.size.height + 8;
    sourceAreaFrame.size.height = 0;
    sourceAreaView.frame = sourceAreaFrame;
    sourceAreaView.layer.borderWidth = 0.5f;
    sourceAreaView.layer.borderColor = [UIColor colorWithRed:230.0 / 255.0 green:230.0 / 255.0 blue:230.0 / 255.0 alpha:1].CGColor;
    if (self.feed.originalFeed != nil) {
        // Text
        CGRect textRect = CGRectMake(8, 8, sourceAreaFrame.size.width - 16, 0);
        UILabel* srcTextLabel = [[UILabel alloc] initWithFrame:textRect];
        srcTextLabel.textAlignment      = UITextAlignmentLeft;
        srcTextLabel.textColor          = [UIColor blackColor];
        srcTextLabel.backgroundColor    = [UIColor clearColor];
        srcTextLabel.font               = [UIFont systemFontOfSize:15.0f];
        srcTextLabel.numberOfLines      = 0;
        srcTextLabel.text               = [NSString stringWithFormat:@"%@: %@", self.feed.originalFeed.nickname, self.feed.originalFeed.text];
        [srcTextLabel sizeToFit];
        textRect = srcTextLabel.frame;
        textRect.origin.x   = 9;
        textRect.origin.y   = 9;
        srcTextLabel.frame = textRect;
        
        // Pictures
        UIView* srcImageAreaView = [[UIView alloc] initWithFrame:CGRectZero];
        frame = srcImageAreaView.frame;
        frame.origin.x = 8;
        frame.origin.y = srcTextLabel.frame.origin.y + srcTextLabel.frame.size.height;
        srcImageAreaView.frame = frame;
        if (self.feed.originalFeed.pictures != nil && [self.feed.originalFeed.pictures count] > 0) {
            UIImage* img = [UIImage imageNamed:[self.feed.originalFeed.pictures objectAtIndex:0]];
            CGSize picSize = img.size;
            frame.size = CGSizeMake(320 - 10 - 10 - 8 - 8, picSize.height * (320 - 10 - 10 - 8 - 8) / picSize.width);
            CGRect srcImageAreaFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
            
            UIScrollView* srcImageContainerView = [[UIScrollView alloc] initWithFrame:srcImageAreaFrame];
            srcImageContainerView.userInteractionEnabled = YES;
            srcImageContainerView.pagingEnabled = YES;
            srcImageContainerView.scrollEnabled = YES;
            srcImageContainerView.bounces = YES;
            srcImageContainerView.tag = 10;
            
            srcImageContainerView.contentSize = CGSizeMake(self.feed.originalFeed.pictures.count * frame.size.width, frame.size.height);
            srcImageContainerView.delegate = self;
            NSLog(@"frame : %f, %f\ncontentSize : %f, %f", srcImageAreaView.frame.size.width, srcImageAreaView.frame.size.height, srcImageContainerView.contentSize.width, srcImageContainerView.contentSize.height);

            // Add pictures
            for (int i = 0; i < [self.feed.originalFeed.pictures count]; i++) {
                NSString* picName = [self.feed.originalFeed.pictures objectAtIndex:i];
                UIImage* img = [UIImage imageNamed:picName];
                CGRect imgRect = CGRectMake(i * frame.size.width, 0, frame.size.width, frame.size.height);
                UIImageView* imgView = [[UIImageView alloc] initWithFrame:imgRect];
                imgView.image = img;
                imgView.userInteractionEnabled = YES;
                
                UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)];
                [imgView addGestureRecognizer:tapGestureRecognizer];
                [tapGestureRecognizer release];
                
                [srcImageContainerView addSubview:imgView];
                [imgView release];
            }
            
            UIPageControl* srcImageIndexIndicator = [[UIPageControl alloc] init];
            srcImageIndexIndicator.numberOfPages = [self.feed.originalFeed.pictures count];
            CGRect imgIndicatorFrame = CGRectMake(srcImageAreaFrame.size.width - 35 /*srcImageIndexIndicator.frame.size.width*/, srcImageAreaFrame.size.height - 20 /*srcImageIndexIndicator.frame.size.height*/, srcImageIndexIndicator.frame.size.width, srcImageIndexIndicator.frame.size.height);
            srcImageIndexIndicator.frame = imgIndicatorFrame;
            srcImageIndexIndicator.tag = 20;
            
            frame = srcImageAreaView.frame;
            frame.size.width = srcImageContainerView.frame.size.width;
            frame.size.height = srcImageContainerView.frame.size.height;
            srcImageAreaView.frame = frame;
            
            [srcImageAreaView addSubview:srcImageContainerView];
            [srcImageContainerView release];
            [srcImageAreaView addSubview:srcImageIndexIndicator];
            [srcImageIndexIndicator release];
        }
        else {
            frame = imageAreaView.frame;
            frame.size = CGSizeZero;
            imageAreaView.frame = frame;
        }
        
        frame = sourceAreaView.frame;
        frame.size.height = 8 + srcTextLabel.frame.size.height + 8 + srcImageAreaView.frame.size.height;
        sourceAreaView.frame = frame;
        sourceAreaView.backgroundColor = [UIColor colorWithRed:249.0 / 255.0 green:249.0 / 255.0 blue:249.0 / 255.0 alpha:1];
        
        [sourceAreaView addSubview:srcTextLabel];
        [srcTextLabel release];
        [sourceAreaView addSubview:srcImageAreaView];
        [srcImageAreaView release];
    }
    
    // Tag area
    UIView* tagAreaView = (UIView*)[bodyView viewWithTag:51];
    CGRect tagAreaFrame = tagAreaView.frame;
    tagAreaFrame.origin.y = sourceAreaView.frame.origin.y + sourceAreaView.frame.size.height + 4;
    tagAreaFrame.size.height = 16;
    tagAreaView.frame = tagAreaFrame;
    // Add tags
    
    NSArray* tags = nil;
    if (self.feed.tags != nil) {
        tags = self.feed.tags;
    }
    else if (self.feed.originalFeed.tags != nil) {
        tags = self.feed.originalFeed.tags;
    }
    if (tags != nil) {
        UIImageView* tagIndicator = (UIImageView*)[tagAreaView viewWithTag:1];
        CGFloat xStart = tagIndicator.frame.origin.x + tagIndicator.frame.size.width + 4;
        for (int i = 0; i < [tags count]; i++) {
        //    int tagIndex = [[tags objectAtIndex:i] intValue];
        //    NSString* tagName = [Utility tagNameFromIndex:tagIndex];
            NSString* tagName = [tags objectAtIndex:i];
            
            CGRect labelRect = CGRectMake(xStart, 0, 0, 12);
            UILabel* tagLabel = [[UILabel alloc] initWithFrame:labelRect];
            tagLabel.textAlignment  = UITextAlignmentLeft;
            tagLabel.textColor      = [UIColor colorWithRed:0xbb / 255.0 green:0xbb / 255.0 blue:0xbb / 255.0 alpha:1];
            tagLabel.backgroundColor    = [UIColor clearColor];
            tagLabel.font           = [UIFont systemFontOfSize:12.0f];
            tagLabel.text           = tagName;
            tagLabel.numberOfLines  = 1;
            CGSize size = [tagName sizeWithFont:tagLabel.font constrainedToSize:CGSizeMake(1000.0f, 16.0f)];
            labelRect.origin.x  = xStart;
            labelRect.origin.y  = 0;
            labelRect.size.width = size.width;
            labelRect.size.height = 16;
            tagLabel.frame = labelRect;
        
            xStart += tagLabel.frame.size.width + 8;
            
            tagLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickTagLabel:)];
            [tagLabel addGestureRecognizer:tapGestureRecognizer];
            [tapGestureRecognizer release];
            
            [tagAreaView addSubview:tagLabel];
            [tagLabel release];
        }
    }
    
    // Time and comment area
    UIView* timeAreaView = (UIView*)[bodyView viewWithTag:61];
    CGRect timeRect = timeAreaView.frame;
    timeRect.origin.y   = tagAreaView.frame.origin.y + tagAreaView.frame.size.height + 6;
    timeAreaView.frame = timeRect;
    
    UILabel* timeLabel = (UILabel*)[timeAreaView viewWithTag:1];
    timeLabel.text = [Utility transTimeToCurrentFormat:self.feed.publishTime];
    
    UILabel* commentLabel = (UILabel*)[timeAreaView viewWithTag:3];
    commentLabel.text = [NSString stringWithFormat:@"%lld", self.feed.commentNum];
    commentLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer* commentTagRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCommentLabel:)];
    [commentLabel addGestureRecognizer:commentTagRecognizer];
    [commentTagRecognizer release];
    
    UILabel* forwardLabel = (UILabel*)[timeAreaView viewWithTag:5];
    forwardLabel.text = [NSString stringWithFormat:@"%lld", self.feed.forwardNum];
    forwardLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer* forwardTagRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickForwardLabel:)];
    [forwardLabel addGestureRecognizer:forwardTagRecognizer];
    [forwardTagRecognizer release];
    
    UILabel* favLabel = (UILabel*)[timeAreaView viewWithTag:7];
    favLabel.text = [NSString stringWithFormat:@"%lld", self.feed.favorNum];
    favLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer* favTagRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickLikeLabel:)];
    [favLabel addGestureRecognizer:favTagRecognizer];
    [favTagRecognizer release];
    
    // Adjust the height of the whole view
    CGRect wholeViewRect = bodyView.frame;
    wholeViewRect.size.height = timeAreaView.frame.origin.y + timeAreaView.frame.size.height + 8;
    wholeViewRect.origin.y = 0;
    bodyView.frame = wholeViewRect;
    // Set border
    bodyView.layer.borderWidth = 0.5f;
    bodyView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    NSLog(@"%@", bodyView);
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:10];
    tableView.tableHeaderView = bodyView;
    
    UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBody:)];
    [bodyView addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];
}

- (void)loadCommentMsg {
    
    if (self.feed == nil) {
        return;
    }
    
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    if (self.feed.originalFeed == nil) {
        self.commentMsgList = [sharedDataManager commentListOfFeed:self.feed];
    }
    else {
        self.commentMsgList = [sharedDataManager commentListOfFeed:self.feed.originalFeed];
    }
    
    // Refresh the table
    UITableView* tableView = (UITableView*)[self.view viewWithTag:10];
    [tableView reloadData];
}

@end
